package com.topdt.jbpm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.util.AppContext;
import com.topdt.jbpm.dictionary.Dictionary;
import com.topdt.jbpm.dictionary.DictionaryValue;
import com.topdt.jbpm.form.SysParamService;
import com.topdt.jbpm.form.query.Option;
import com.topdt.jbpm.form.query.Param;
import com.topdt.jbpm.identity.Company;
import com.topdt.jbpm.identity.Department;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.OptionImpl;
import com.topdt.jbpm.model.ParamImpl;
import com.topdt.jbpm.utils.SelectConstant;

/**
 * 部门管理的 service
 * 建立日期 : Apr 8, 2013 2:56:47 PM<br>
 * 作者 : 孙耀飞<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Service
@Transactional(readOnly = true)
public class SysParamImplService implements SysParamService {
	/**
	 * 取得系统中所有平级的变量
	 */
	@Override
	public List<Param> getAllFlatParam(String type) {
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		String con = SelectConstant.DICTIONARY_CODE;
		if("more".equals(type)){
			con += SelectConstant.DICTIONARY_MORE ;
		}else if("single".equals(type)){
			con += SelectConstant.DICTIONARY_SINGLE ;
		}
		List<Dictionary> dList = idService.searchByCode(con);
		List<Param> pList = new ArrayList<Param>();
		for(Dictionary value:dList){
			ParamImpl param = new ParamImpl();
			param.setId(value.getId());
			param.setName(value.getName());
			pList.add(param);
		}
		return pList;
	}

	/**
	 * 取得系统中所有分级的变量
	 */
	@Override
	public List<Param> getAllHierarchicalParam() {
		List<Param> list = new ArrayList<Param>();
		ParamImpl param = new ParamImpl();
		param.setName("组织机构");
		param.setId("org");
		list.add(param);
		return list;
	}

	/**
	 * 取得系统中的所有字典参数的类型
	 */
	@Override
	public List<Param> getAllParam() {
		List<Param> pList = new ArrayList<Param>();
		List<Param> flatParam = getAllFlatParam("");
		List<Param> hierarchicalParam = getAllHierarchicalParam();
		if(flatParam!=null && flatParam.size()>0){
			pList.addAll(flatParam);
		}
		if(hierarchicalParam!=null&& hierarchicalParam.size()>0){
			pList.addAll(hierarchicalParam);
		}
		return pList;
	}
	/**
	 * 根据变量的id 和类型取到其所有的备选值
	 */
	@Override
	public List<Option> getOptions(String id,String type) {

		List<Option> pList = new ArrayList<Option>();
		if("more".equals(type)&&StringUtils.isNotBlank(id)){
			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			List<DictionaryValue> dics = idService.findByDicnId(id);
			if(dics!=null&&dics.size()>0){
				for(DictionaryValue d :dics){
					OptionImpl option = new OptionImpl();
					option.setName(d.getText());
					option.setValue(d.getValue());
					option.setId(d.getId());
					pList.add(option);
				}
			}

		}else if("tree".equals(type)){
			String comId = AppContext.getCurrentComId();
			if(id.equals("org")){
				//--组织结构的list
				IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
				List<Department> deps = idService.getAllDepsAndUsers(comId);
				pList = getOptionsByDepartment(deps);
			}

		}else if("system".equals(type)){
			pList.add(SelectConstant.getOptionByParamId(id));
		}

		return pList;
	}




	@Override
	public List<Param> getAllSystemParam() {
		return SelectConstant.getSystemSelect();
	}

	public List<Option> getOptionsByDepartment(List<Department> deps) {

		Department dep = deps.get(0);

		Option op = converDeps2Options(dep);

		List<Option> opsList=new ArrayList<Option>();
		opsList.add(op);

		return opsList;
	}
	private Option converDeps2Options(Department dep){

		OptionImpl option=new OptionImpl();

		option.setId(dep.getId());
		option.setName(dep.getName());

		List<? extends Department> children = dep.getChildren();

		List<Option> depsOP=new ArrayList<Option>();

		if(children!=null){
			for(Department depTemp:children){
				Option op =converDeps2Options(depTemp);
				depsOP.add(op);
			}
		}

		@SuppressWarnings("unchecked")
		List<User> users = (List<User>) dep.getUsers();

		List<Option> userOp=new ArrayList<Option>();

		if(users!=null){
			for(User user:users){
				OptionImpl op=new OptionImpl();
				op.setId(user.getId());
				op.setName(user.getName());
				userOp.add(op);
			}
		}
		List<Option> childrenOp=new ArrayList<Option>();

		childrenOp.addAll(depsOP);
		childrenOp.addAll(userOp);

		option.setChildren(childrenOp);

		return option;
	}

	@Override
	public Option getOptionName(String id, String type) {
		if("tree".equals(type)){
			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			Department dep = idService.getDeptById(id);
			if(dep==null){
				Company ent = idService.getCompById(id);
				OptionImpl op=new OptionImpl();
				op.setId(id);
				op.setName(ent.getName());
				return op;
			}else{
				OptionImpl op=new OptionImpl();
				op.setId(dep.getId());
				op.setName(dep.getName());
				return op;
			}

		}else if("more".equals(type)){
			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			Dictionary dic = idService.findDictById(id);
			OptionImpl op=new OptionImpl();
			op.setId(dic.getId());
			op.setName(dic.getName());
			op.setValue(dic.getCode());
			return op;
		}else if("system".equals(type)){
			return SelectConstant.getOptionByParamId(id);
		}

		return null;
	}
}
